/********************************************************************
* Name: ConstExpEvalFilter.h: 
*
* Description:
*   Doing constant evaluation (constant folding for VInst)
*
* History:
*   Create: gxf. 2008-11-15
*
********************************************************************/
#ifndef CONST_EXP_EVAL_FILTER_H
#define CONST_EXP_EVAL_FILTER_H

#include "crossbit/VFilter.h"
#include "crossbit/VBlock.h"
#include "crossbit/GLOBAL.h"

#include <map>

namespace crossbit{

class ConstExpEvalFilter : public VFilter{ 
    public:
        // nontrivial constructor
        ConstExpEvalFilter();
    public:
        XTUint32 run(VBlock *);

        inline static XTUint32 GetInstReduced(){
            return optCnt - addCnt;
        }

    protected: 
        void PerformEval(XTUint32 val1, XTUint32 val2, XTRegNum reg, 
                         VBlock::vb_iterator& Itr, VBlock* pBlock);

        void PerformEval2(XTUint32 val1, XTUint32 val2, 
                         XTRegNum reg1, XTRegNum reg2, 
                         VBlock::vb_iterator& Itr, VBlock* pBlock);

        void DeleteInst(VInst* curInst, VBlock* pBlock, VBlock::vb_iterator& Itr);

    protected: 
        // Notice, currently, CrossBit just support 32bit immediate value
        // table to hold <vreg(key), VInst*> pair
        typedef std::map<XTRegNum, VInst*>::iterator TB_ITR;
        std::map<XTRegNum, VInst*> table;
        XTInt32     tags[GLOBAL::MAX_VREG];
    private:
        static XTUint32    optCnt;
        static XTUint32    addCnt;
};

}

#endif
